/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.model;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.text.AttributeSet;
import netscape.javascript.JSObject;

/**
 *
 * @author
 * Walter
 * Lütgenau
 */
public class JSAttributeSet implements AttributeSet {
    HashMap<String, String> attributes = new HashMap<String, String>();
    
    public JSAttributeSet(JSObject attributesObject) {
        if (attributesObject == null)
            return;
        
        int length = getInt(attributesObject.getMember(JSNode.M_LENGTH));
        for (int i = 0; i < length; i++) {
            JSObject attribute = (JSObject) attributesObject.getSlot(i);
            
            String key = attribute.getMember(JSNode.M_NODE_NAME).toString();
            String value = attribute.getMember(JSNode.M_VALUE).toString();
            
            attributes.put(key, value);
        }
    }
    
    protected int getInt(Object obj) {
        return new Float(obj.toString()).intValue();
    }    
    
    class IteratorEnumeration implements Enumeration {
        Iterator iterator;
        
        public IteratorEnumeration(Iterator iterator) {
            this.iterator = iterator;
        }

        @Override
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        @Override
        public Object nextElement() {
            return iterator.next();
        }
        
    }

    @Override
    public int getAttributeCount() {
        return attributes.size();
    }

    @Override
    public boolean isDefined(Object attrName) {
        return attributes.containsKey(attrName);
    }

    @Override
    public boolean isEqual(AttributeSet attr) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AttributeSet copyAttributes() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object getAttribute(Object key) {
        return attributes.get(key);
    }

    @Override
    public Enumeration<?> getAttributeNames() {
        return new IteratorEnumeration(attributes.keySet().iterator());
    }

    @Override
    public boolean containsAttribute(Object name, Object value) {
        Object result = attributes.get(name);
        return result != null && result.equals(value);
    }

    @Override
    public boolean containsAttributes(AttributeSet attributes) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AttributeSet getResolveParent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
